package com.syni.mdd.sjb.community.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbUserCount;
import com.syni.mdd.sjb.common.component.mybatis.community.mapper.SjbUserCountMapper;
import com.syni.mdd.sjb.common.component.utils.common.exception.BusinessException;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.community.base.entity.vo.UserCountRangeDataVO;
import com.syni.mdd.sjb.community.base.entity.vo.UserDataCountVO;
import com.syni.mdd.sjb.community.base.service.SjbUserCountService;
import com.syni.mdd.sjb.community.base.utils.MybatisUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wangbaitao
 * @version 1.0.0
 * <h>企业微信用户统计数据实体 service impl</h>
 * @Date 2021/3/3
 **/
@Service
@Transactional
public class SjbUserCountServiceImpl extends ServiceImpl<SjbUserCountMapper, SjbUserCount>
        implements SjbUserCountService {
    /**
     * 获取上一日的统计数据
     *
     * @return 查询结果
     */
    @Override
    public Map<String, Object> getUserCount(String corpId) {
        SjbUserCount sjbUserCount = getOne(new QueryWrapper<SjbUserCount>()
                .eq(SjbUserCount.CORP_ID, corpId).eq(SjbUserCount.STAT_TIME, LocalDate.now().minusDays(1)));
        UserDataCountVO userDataCountVO = new UserDataCountVO();
        if (Objects.nonNull(sjbUserCount)) {
            userDataCountVO.setTotal(sjbUserCount.getTotalCount());
            userDataCountVO.setAddCount(sjbUserCount.getNewContactCnt());
            userDataCountVO.setSubCount(sjbUserCount.getNegativeFeedbackCnt());
        }
        return ResultMapHelper.success(userDataCountVO, "获取成功", true);
    }

    /**
     * 获取日期区间的统计i数据
     *
     * @param corpId    企业id
     * @param beginDate 开始日期
     * @param endDate   结束日期
     * @return 查询结果
     */
    @Override
    public Map<String, Object> getUserCountByDate(String corpId, String beginDate, String endDate) {
        if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate) || StringUtils.isBlank(corpId)) {
            throw new BusinessException("业务异常：参数不足");
            //return ResultMapHelper.result(ResultCode.CODE_ERROR_100,"业务异常：参数不足");
        }
        List<SjbUserCount> userCounts = list(new QueryWrapper<SjbUserCount>().eq(SjbUserCount.CORP_ID, corpId)
                .between(SjbUserCount.STAT_TIME, MybatisUtils.getBeginDay(beginDate), MybatisUtils.getEndDay(endDate))
                .orderByAsc(SjbUserCount.STAT_TIME));
        List<UserDataCountVO> total = new ArrayList<>();
        List<UserDataCountVO> add = new ArrayList<>();
        List<UserDataCountVO> sub = new ArrayList<>();
        userCounts.forEach(userCount -> {
            UserDataCountVO userDataCountVOTotal = new UserDataCountVO();
            userDataCountVOTotal.setStatDate(userCount.getStatTime());
            userDataCountVOTotal.setCount(userCount.getTotalCount());
            total.add(userDataCountVOTotal);
            UserDataCountVO userDataCountVOAdd = new UserDataCountVO();
            userDataCountVOAdd.setStatDate(userCount.getStatTime());
            userDataCountVOAdd.setCount(userCount.getNewContactCnt());
            add.add(userDataCountVOAdd);
            UserDataCountVO userDataCountVOSub = new UserDataCountVO();
            userDataCountVOSub.setStatDate(userCount.getStatTime());
            userDataCountVOSub.setCount(userCount.getNegativeFeedbackCnt());
            sub.add(userDataCountVOSub);
        });
        return ResultMapHelper.success(UserCountRangeDataVO.builder().total(total).add(add).sub(sub).build(),
                "获取成功", true);
    }
}
